package com.hula.loan4u.net;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hula.loan4u.bean.BannerBean;
import com.hula.loan4u.bean.BaseResponse;
import com.hula.loan4u.bean.CategoryBean;
import com.hula.loan4u.bean.GoodsBean;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class NetManager {
    private static NetManager instance;
    private static Gson gson;
    public static final String FORMAL_HOST = "http://loan.hulaup.com";
    public static String HOST = FORMAL_HOST;

    private NetManager() {
    }

    public static NetManager getInstance() {
        if (instance == null) {
            instance = new NetManager();
        }
        if (gson == null) {
            gson = new Gson();
        }
        return instance;
    }

    private GetBuilder okHttpGet(Context mContext, String apiMethod, Object paras) {
        Map<String, String> parasMap = bean2Map(paras);
        GetBuilder getBuilder = OkHttpUtils.get().tag(mContext).url(HOST + apiMethod).params(parasMap);
        return getBuilder;
    }

    private PostFormBuilder okHttpPost(Context mContext, String apiMethod, Object paras) {
        Map<String, String> parasMap = bean2Map(paras);
        PostFormBuilder postFormBuilder = OkHttpUtils.post().tag(mContext).url(HOST + apiMethod).params(parasMap);
        return postFormBuilder;
    }

    private Map<String, String> bean2Map(Object bean) {
        Map<String, String> params = new HashMap<>();
        try {
            if (bean != null) {
                Class cls = bean.getClass();
                for (; cls != Object.class; cls = cls.getSuperclass()) {
                    Field[] fields = cls.getDeclaredFields();
                    for (Field f : fields) {
                        f.setAccessible(true);
                        String value = String.valueOf(f.get(bean));
                        if (!TextUtils.isEmpty(value)) {
                            params.put(f.getName(), value);
                        }
                    }
                }
            }
        } catch (Exception e) {
        }
        return params;
    }

    public interface NetCallBack<T> {
        void onSuccess(T data);

        void onFail(String message);
    }

    public void getBanner(Context mContext, boolean showLoading, final NetCallBack<List<BannerBean>> callBack) {
        if (callBack == null) {
            return;
        }
        okHttpGet(mContext, "/home/banner", null)
                .build().execute(new LoadingStringCallBack(mContext, showLoading) {
            @Override
            public void onError(String errorMessage) {
                callBack.onFail(errorMessage);
            }

            @Override
            public void onResponse(String response) {
                try {
                    BaseResponse<List<BannerBean>> bean = gson.fromJson(response, new TypeToken<BaseResponse<List<BannerBean>>>() {
                    }.getType());
                    if (bean.code == 0) {
                        callBack.onSuccess(bean.data);
                    } else
                        callBack.onFail(bean.message);
                } catch (Exception e) {
                    callBack.onFail("");
                }
            }
        });
    }

    public void getGoods(int page, Context mContext, boolean showLoading, final NetCallBack<List<GoodsBean>> callBack) {
        if (callBack == null) {
            return;
        }
        okHttpGet(mContext, "/home/recommend", null)
                .addParams("page", String.valueOf(page))
                .addParams("size", "10")
                .build().execute(new LoadingStringCallBack(mContext, showLoading) {
            @Override
            public void onError(String errorMessage) {
                callBack.onFail(errorMessage);
            }

            @Override
            public void onResponse(String response) {
                try {
                    BaseResponse<List<GoodsBean>> bean = gson.fromJson(response, new TypeToken<BaseResponse<List<GoodsBean>>>() {
                    }.getType());
                    if (bean.code == 0) {
                        callBack.onSuccess(bean.data);
                    } else
                        callBack.onFail(bean.message);
                } catch (Exception e) {
                    callBack.onFail("");
                }
            }
        });
    }
    public void getCategory( Context mContext, boolean showLoading, final NetCallBack<List<CategoryBean>> callBack) {
        if (callBack == null) {
            return;
        }
        okHttpGet(mContext, "/home/category", null)
                .build().execute(new LoadingStringCallBack(mContext, showLoading) {
            @Override
            public void onError(String errorMessage) {
                callBack.onFail(errorMessage);
            }

            @Override
            public void onResponse(String response) {
                try {
                    BaseResponse<List<CategoryBean>> bean = gson.fromJson(response, new TypeToken<BaseResponse<List<CategoryBean>>>() {
                    }.getType());
                    if (bean.code == 0) {
                        callBack.onSuccess(bean.data);
                    } else
                        callBack.onFail(bean.message);
                } catch (Exception e) {
                    callBack.onFail("");
                }
            }
        });
    }


}
